#include <vcl.h>
#include "TAfin.h"
#pragma package(smart_init)

TAfin::TAfin() {
    mat[0] = 1;    mat[4] = 0;    mat[ 8] = 0;    mat[12] = 0;
    mat[1] = 0;    mat[5] = 1;    mat[ 9] = 0;    mat[13] = 0;
    mat[2] = 0;    mat[6] = 0;    mat[10] = 1;    mat[14] = 0;
    mat[3] = 0;    mat[7] = 0;    mat[11] = 0;    mat[15] = 1;
};

TAfin::TAfin(float m[16]){
    for (int i=0; i<16; i++)
        mat[i] = m[i];
};

void TAfin::postMultiplicar(TAfin* matriz){
    float otraMatriz[16];
    for(int i=0;i<16;i++)
        otraMatriz[i]= this->mat[i];

    float a=0;

    for ( int i = 0 ; i<4 ; i++){
        for(int j=0;j<4;j++){
            a=0;
            for(int k=0;k<4;k++){
                a += (otraMatriz[j+(k*4)] * matriz->mat[(i*4)+k]);
            }
            mat[(i*4)+j] = a;
        }
    }
}


void TAfin::traslacion(float vectorX,float vectorY,float vectorZ){
    TAfin* aux = new TAfin();


    aux->mat[0]  =1 ;   aux->mat[4]  =0 ;   aux->mat[8]  =0 ;   aux->mat[12] = vectorX ;
    aux->mat[1]  =0 ;   aux->mat[5]  =1 ;   aux->mat[9]  =0 ;   aux->mat[13] = vectorY;
    aux->mat[2]  =0 ;   aux->mat[6]  =0 ;   aux->mat[10] =1 ;   aux->mat[14] = vectorZ;
    aux->mat[3]  =0 ;   aux->mat[7]  =0 ;   aux->mat[11] =0 ;   aux->mat[15] = 1;

  postMultiplicar(aux);
}

//---------------------------------------------------------------------------

void TAfin::rotacionX(float angulo) {
    TAfin* auxiliar = new TAfin();
    float coseno = cos(angulo);
    float seno = sin(angulo);

    auxiliar->mat[0]  =1;  auxiliar->mat[1]  =0;      auxiliar->mat[2]  =0;       auxiliar->mat[3]  =0 ;
    auxiliar->mat[4]  =0;  auxiliar->mat[5]  =coseno; auxiliar->mat[6]  =-seno;   auxiliar->mat[7]  =0 ;
    auxiliar->mat[8]  =0;  auxiliar->mat[9]  =seno;   auxiliar->mat[10] =coseno ; auxiliar->mat[11] =0 ;
    auxiliar->mat[12] =0;  auxiliar->mat[13] =0;      auxiliar->mat[14] =0 ;      auxiliar->mat[15] =1 ;


    postMultiplicar(auxiliar);
}

//---------------------------------------------------------------------------

void  TAfin::rotacionY(float ang){
    TAfin* aux = new TAfin();
    float coseno = cos(ang);
    float seno = sin(ang);

    aux->mat[0]  = coseno ;  aux->mat[1]  =0 ;   aux->mat[2]  =seno ;  aux->mat[3]  =0 ;
    aux->mat[4]  =0 ;  aux->mat[5]  =1 ;   aux->mat[6]  =0 ;  aux->mat[7]  =0 ;
    aux->mat[8]  =-seno;  aux->mat[9]  =0 ;   aux->mat[10] =coseno ;  aux->mat[11] =0 ;
    aux->mat[12] =0 ;  aux->mat[13] =0 ;   aux->mat[14] =0 ;  aux->mat[15] =1 ;


    postMultiplicar(aux);
}

//---------------------------------------------------------------------------

void  TAfin::rotacionZ(float ang){
    TAfin* aux = new TAfin();
    float c=cos(ang);
    float s=sin(ang);

    aux->mat[0]  =c ; aux->mat[1]  =-s; aux->mat[2]  =0 ;  aux->mat[3]  =0 ;
    aux->mat[4]  =s ; aux->mat[5]  =c ; aux->mat[6]  =0 ;  aux->mat[7]  =0 ;
    aux->mat[8]  =0 ; aux->mat[9]  =0 ; aux->mat[10] =1 ;  aux->mat[11] =0 ;
    aux->mat[12] =0 ; aux->mat[13] =0 ; aux->mat[14] =0 ;  aux->mat[15] =1 ;

    postMultiplicar(aux);
}

//---------------------------------------------------------------------------

void  TAfin::escalacion(float vX,float vY,float vZ){
    TAfin* aux=new TAfin();

    aux->mat[0]  =vX ;  aux->mat[1]  =0 ;   aux->mat[2]  =0 ;   aux->mat[3]  =0 ;
    aux->mat[4]  =0 ;   aux->mat[5]  =vY ;  aux->mat[6]  =0 ;   aux->mat[7]  =0 ;
    aux->mat[8]  =0;    aux->mat[9]  =0 ;   aux->mat[10] =vZ ;  aux->mat[11] =0 ;
    aux->mat[12] =0 ;   aux->mat[13] =0 ;   aux->mat[14] =0 ;   aux->mat[15] =1 ;


    postMultiplicar(aux);
}
